import cv2
import pytesseract

# 加载验证码图片
image_path = r"C:\Users\10352\Desktop\GenerateCaptcha.png"  # 替换为实际的图片路径
image = cv2.imread(image_path)

# 将图像转换为灰度图像
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

# 对图像进行二值化处理
_, binary_image = cv2.threshold(gray_image, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)

# 去除干扰线
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (3, 3))
clean_image = cv2.morphologyEx(binary_image, cv2.MORPH_OPEN, kernel)

# 使用 Tesseract OCR 进行数字识别
code = pytesseract.image_to_string(clean_image, config='--psm 10 --oem 3 -c tessedit_char_whitelist=0123456789')
print(code)

